package cn.sjxy.apiAdmin;

import cn.sjxy.baseLib.AjaxResult;
import cn.sjxy.model.Md_Admin;
import cn.sjxy.service.Sv_Admin;
import cn.sjxy.service.Sv_Login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/*管理员登录*/
@RestController
@Validated
public class Ad_Login {

    @Autowired
    private Sv_Admin sv_admin;
    @Autowired
    private Sv_Login sv_login;

    //登录
    @PostMapping(value = "/login/adminLogin")
    public AjaxResult adminLogin(@NotBlank(message = "用户名必填") String userCode, @NotBlank(message = "密码必填") String password) throws Exception {
        Md_Admin user = sv_admin.checkLogin(userCode, password);
        String token = sv_login.saveAdmin(userCode, password);
        AjaxResult result = AjaxResult.success("登录成功");
        result.addKey("token", token);
        return result;
    }

    //退出
    @PostMapping(value = "/login/adminLogout")
    public AjaxResult adminLogout(String token) {
        sv_login.logout(token);
        return AjaxResult.success("退出成功");
    }
}
